package com.zrgj.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zrgj.common.response.BaseResponse;
import com.zrgj.common.response.StatusCodeEnum;
import com.zrgj.domain.*;
import com.zrgj.manager.service.IProductInfoService;
import com.zrgj.manager.service.IProductSalesService;
import com.zrgj.manager.service.IProductService;
import org.apache.commons.collections.ListUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class ProductController {
    @Reference
    private IProductService productService;
    @Reference
    private IProductInfoService productInfoService;
    @Reference
    private IProductSalesService productSalesService;

    /**
     * 最新商品
     * @return
     */
    @RequestMapping(value = "/new")
    public BaseResponse newProList(){
        List<Product> products = productService.selectNew();
        return new BaseResponse(StatusCodeEnum.SUCCESS,products);
    }

    /**
     * 精选商品
     * @return
     */
    @RequestMapping(value = "/select")
    public BaseResponse selectProList(){
        List<Product> products = productService.selected();
        return new BaseResponse(StatusCodeEnum.SUCCESS,products);
    }

    /**
     * 畅销
     * @return
     */
    @RequestMapping(value = "/bestSell")
    public BaseResponse bestSellProList(){
        List<ProductVo> products = productService.selectBest();
        return new BaseResponse(StatusCodeEnum.SUCCESS,products);
    }

    /**
     * 商品详情设置
     * @param id
     * @param response
     * @return
     */
    @RequestMapping(value = "/setDetails")
    public BaseResponse productDetails(Integer id, HttpServletResponse response){

        Cookie cookie = new Cookie("PRODUCT", id.toString());
        cookie.setPath("/");
        cookie.setMaxAge(1800);
        response.addCookie(cookie);

        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    /**
     * 商品详情显示
     * @param productId
     * @return
     */
    @RequestMapping(value = "/showDetails")
    public BaseResponse showDetails(@RequestParam(required = false) Integer productId, HttpServletResponse response){
        Product  product = new Product();
        ProductInfo productInfo = new ProductInfo();
        ProductAndProductInfo productAndProductInfo = new ProductAndProductInfo();
        if (productId != null && productId != -1){
            product.setProductId(productId);
            List<Product> showProduct = productService.selectPro(product);
            productInfo.setProductId(showProduct.get(0).getProductId());
            List<ProductInfo> select = productInfoService.select(productInfo);
            productAndProductInfo.setProduct(showProduct.get(0));
            productAndProductInfo.setProductInfo(select.get(0));
            return new BaseResponse(StatusCodeEnum.SUCCESS,productAndProductInfo);
        }else {
            List<Product> productList = productService.selectPro(new Product());
            long l = System.currentTimeMillis();
            int num = (int) (l % productList.size());

            Product product1 = productList.get(num);

            productInfo.setProductId(product1.getProductId());
            List<ProductInfo> select = productInfoService.select(productInfo);

            productAndProductInfo.setProduct(product1);
            productAndProductInfo.setProductInfo(select.get(0));

            Cookie cookie =  new Cookie("PRODUCT", product1.getProductId().toString());
            cookie.setMaxAge(1800);
            cookie.setPath("/");
            response.addCookie(cookie);

            return new BaseResponse(StatusCodeEnum.SUCCESS,productAndProductInfo);
        }

    }

    /**
     * 相关商品列表
     * @param productId
     * @return
     */
    @RequestMapping(value = "/relatedList")
    public BaseResponse relatedList(Integer productId){
        Product product = new Product();
        product.setProductId(productId);
        List<Product> productList = productService.selectPro(product);
        Product relatedProduct = productList.get(0);
        Product p = new Product();
        p.setPrimaryId(relatedProduct.getPrimaryId());
        List<Product> productList1 = productService.selectPro(p);
        return new BaseResponse(StatusCodeEnum.SUCCESS, productList1);
    }

    /**
     * 商品详情
     * @param productId
     * @return
     */
    @RequestMapping(value = "/showProduct")
    public BaseResponse showProduct(Integer productId){
        Map<String,Object> map = new HashMap<>();
        Product product = new Product();
        ProductInfo productInfo = new ProductInfo();
        ProductSales productSales = new ProductSales();

        product.setProductId(productId);
        productInfo.setProductId(productId);
        productSales.setProductId(productId);

        List<Product> productList = productService.selectPro(product);
        List<ProductInfo> select = productInfoService.select(productInfo);
        List<ProductInfo> select1 = productSalesService.select(productSales);

        map.put("product",productList.get(0));
        map.put("productInfo",select.get(0));
        map.put("productSales",select1.get(0));


        return new BaseResponse(StatusCodeEnum.SUCCESS,map);
    }

    /**
     * 显示时尚列表
     * @param primaryId
     * @return
     */
    @RequestMapping(value = "/fashion")
    public BaseResponse fashionList(Integer primaryId){
        List<Product> productList = productService.selectByPrimary(primaryId);
        return new BaseResponse(StatusCodeEnum.SUCCESS,productList);
    }

    /**
     * 热卖列表
     * @param primaryId
     * @return
     */
    @RequestMapping(value = "/sell")
    public BaseResponse sellList(Integer primaryId){
        List<Product> productList = productService.selectSell(primaryId);
        return new BaseResponse(StatusCodeEnum.SUCCESS,productList);
    }

    /**
     * 设置商品类型
     * @param primaryId
     * @param response
     * @return
     */
    @RequestMapping(value = "/setPrimary")
    public BaseResponse setPrimary(Integer primaryId, HttpServletResponse response){
        Cookie cookie = new Cookie("PRIMARY",primaryId.toString());
        cookie.setMaxAge(1800);
        cookie.setPath("/");
        response.addCookie(cookie);
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    /**
     * 根据商品类型显示商店内容
     * @param primaryId
     * @return
     */
    @RequestMapping(value = "/primaryShop")
    public BaseResponse primaryShop(@RequestParam(required = false) Integer primaryId, @RequestParam(defaultValue = "1") Integer page){
        List<ProductPo> productPos = productService.selectShopPrimary(primaryId);
        List<ProductPo> pageList = getPageList(productPos, page);
        return new BaseResponse(StatusCodeEnum.SUCCESS,pageList);
    }

    /**
     * 设置查找的关键字
     * @param keyWord
     * @param response
     * @return
     */
    @RequestMapping(value = "/setKeyWord")
    public BaseResponse setKeyWord(String keyWord, HttpServletResponse response){
        Cookie cookie = new Cookie("KEYWORD", keyWord);
        cookie.setMaxAge(1800);
        cookie.setPath("/");
        response.addCookie(cookie);

        return new BaseResponse(StatusCodeEnum.SUCCESS,"user_shop");
    }

    /**
     * 获取关键字相关的商品列表
     * @param keyWord
     * @param page
     * @return
     */
    @RequestMapping(value = "/getKeyWordList")
    public BaseResponse getKeyWordList(@RequestParam(required = false) String keyWord, @RequestParam(required = false, defaultValue = "1") Integer page){

        List<ProductPo> productPos = productService.selectKey(keyWord);
        List<ProductPo> pageList = getPageList(productPos, page);

        return new BaseResponse(StatusCodeEnum.SUCCESS,pageList);
    }

    /**
     * 获取当前页的商品列表
     * @param productPos
     * @param page
     * @return
     */
    private List<ProductPo> getPageList(List<ProductPo> productPos, Integer page) {
        if (productPos.size() < 15){
            return productPos;
        }
        if ((productPos.size() - page*15) < 15){
            return productPos.subList((page - 1) * 15, productPos.size());
        }else {
            return productPos.subList((page - 1) * 15, page * 15);
        }
    }
}
